package com.example.courier.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.courier.common.enums.DeliveryStatus;
import com.example.courier.model.req.AcceptOrderDTO;
import com.example.courier.model.req.DeliveryOrderDTO;
import com.example.courier.model.resp.DeliveryOrderVO;
import com.example.courier.service.DeliveryOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2025/9/6 14:37
 **/
@RestController
@RequestMapping("/api/delivery/orders")
@RequiredArgsConstructor
public class DeliverOrderController {
    private final DeliveryOrderService deliveryOrderService;

    @PostMapping("/create")
    public void createDeliveryOrder(@RequestBody DeliveryOrderDTO deliveryOrderDTO) {
        deliveryOrderService.createDeliveryOrder(deliveryOrderDTO);
    }

    @GetMapping("/list")
    public List<DeliveryOrderVO> getDeliveryOrderByUserId() {
        String userId = "1145141919810";
        return deliveryOrderService.getDeliveryOrderByUserId(userId);
    }

    @GetMapping("/destination")
    public List<DeliveryOrderVO> getStartToEndOrders(@RequestParam(value = "start", required = false) String startLocationId,
                                                     @RequestParam(value = "end", required = false) String endLocationId,
                                                     @RequestParam(value = "status", required = false) String status) {
        return deliveryOrderService.getStartToEndOrders(startLocationId, endLocationId, status);
    }

    @PostMapping("/accept/batch")
    public void acceptOrdersBatch(@RequestBody AcceptOrderDTO acceptOrderDTO) {
        deliveryOrderService.changeOrders("sf1145141919819", acceptOrderDTO.getOrderIds(), DeliveryStatus.IN_PROGRESS);
    }

    @PostMapping("/{orderId}/accept")
    public void acceptOrders(@PathVariable String orderId) {
        deliveryOrderService.changeOrders("sf1145141919819", Collections.singletonList(orderId), DeliveryStatus.IN_PROGRESS);
    }

    @PostMapping("/complete/batch")
    public void completeOrdersBatch(@RequestBody AcceptOrderDTO acceptOrderDTO) {
        deliveryOrderService.changeOrders("sf1145141919819", acceptOrderDTO.getOrderIds(), DeliveryStatus.IN_PROGRESS);
    }

    @PostMapping("/{orderId}/complete")
    public void completeOrders(@PathVariable String orderId) {
        deliveryOrderService.changeOrders("sf1145141919819", Collections.singletonList(orderId), DeliveryStatus.DELIVERED);
    }

}
